/**
 * 物联卡账户充值交费对话框
 */

layui.use(['layer', 'form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;

    /**
     * 查询所有的账号信息，填充select标签
     */
    function searchAccount(accountId) {
        $('#accountId').html('');
        $("#accountId").append('<option value="" >请选择</option>');
        $.ajax({
            type: 'get',
            url: Feng.ctxPath + "/account/allSelectAccounts",
            async: false,
            success: function (data) {
                for (var i = 0; i < data.data.length; i++) {
                    var name = data.data[i].accountName;
                    var code = data.data[i].accountId;
                    if (accountId == code) {
                        $("#accountId").append('<option value="' + code + '" selected>' + name + '</option>');
                    } else {
                        $("#accountId").append('<option value="' + code + '">' + name + '</option>');
                    }
                }
                form.render('select');
            }
        });
    }
    searchAccount();

    form.verify({
        accountId: function (value) {
            if($("#entityType").val() === "2"){
                if(value === "") {
                    return '充值标识类型为2时必填';
                }
            }
        },
        chargeMoney: [/^[0-9]+(.?[0-9]{1,2})?$/,"请输入纯数字，支持两位小数"],
        defaultBank: function (value) {
            if($("#paymentType").val() === "CMPAY-BANK"){
                if(value === "") {
                    return '在支付方式为CMPAY-BANK:和包-网银时必填';
                }
            }
        }
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        $.ajax({
            dataType : "json",
            url : Feng.ctxPath + "/generalRecharge/rechargeSimAccount",
            type : "post",
            data : $("#cardForm").serialize(),
            success : function(data) {
                if(data.success){
                    layer.open({
                        type: 0,
                        title: '物联卡账户充值交费办理结果',
                        area: ['550px', '450px'],
                        content: '<label>充值订单号:</label>' + data.data.orderNo + '<br>'
                                + '<label>支付链接:</label>' + '<a href="' + data.data.url+ '" target="_blank" style="color: #0000ee;text-decoration:underline">'+ data.data.url +'</a>' + '<label>(通过此地址发起支付)</label>',
                        skin: 'layui-layer-molv',
                        yes: function (index, layero) {
                            admin.closeThisDialog();
                        }
                    });
                }
            },
            error : function (data){
                layer.open({
                    content: "充值交费失败！" + data.responseJSON.message,
                    skin: 'layui-layer-molv',
                    yes: function (index, layero) {
                        layer.close(index);
                    }
                });
            }
        });
        return false;
    });

    form.on('select(entityType)', function(){
        if($("#entityType").val() === "1"){
            $('#divAccount').hide();
        }else{
            $('#divAccount').show();
        }
    });
});